package com.songshuai.sales.client.service.impl;

import com.songshuai.sales.client.dao.SalesInfoMapper;
import com.songshuai.sales.client.service.SalesInfoService;
import com.songshuai.webapi.baseapi.model.sales.SalesInfo;
import com.songshuai.webapi.baseapi.utils.SnowFlakeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SalesInfoServiceImpl  implements SalesInfoService {

    @Autowired
    private SalesInfoMapper salesInfoMapper;


    @Autowired
    private SnowFlakeUtil snowFlakeUtil;

    @Override
    @Async
    public SalesInfo querySalesInfoByOpenId(String openId) {
       SalesInfo salesInfo= new SalesInfo(){{
            setOpenId(openId);
        }};
        List<SalesInfo> info = salesInfoMapper.selectByEntityList(salesInfo);
        SalesInfo sales= null;
        if(info.size()>0){
            sales=info.get(0);
        }
        return sales;
    }

    @Override
    public SalesInfo querySalesInfoByToken(String token) {
        SalesInfo salesInfo= new SalesInfo(){{
            setToken(token);
        }};
        SalesInfo info = salesInfoMapper.selectByEntity(salesInfo);
        return info;
    }

    @Override
    @Async
    public Integer insertSalesInfoForLogin(SalesInfo salesInfo) {
        salesInfo.setId(snowFlakeUtil.nextId());
        int i = salesInfoMapper.insertSelective(salesInfo);
        return i;
    }

    @Override
    @Async
    public Integer updateSalesInfoByOpenid(SalesInfo salesInfo) {
        int i = salesInfoMapper.updateSalesInfoByOpenid(salesInfo);
        return i;
    }
}
